import axios from '@/axios';

const service = axios.create({
  baseURL: '/image',
});

service.interceptors.response.use(response => {
  const code = response.data.code || 200;
  if (code === 200) {
    return response.data.data;
  }

  let msg = response.data.code + ' ' + response.data.msg;
  ElMessage.error(msg);

  return Promise.reject('上传图片失败：' + msg);
});

// 登录
export function login(user_name, password) {
  return axios.post('/users/login', {
    user_name,
    password,
  });
}
// 获取用户列表
export function list() {
  return axios.get('/users/userlist');
}
// 根据ID获取用户信息
export function getuser(id) {
  return axios.get('/users' + id);
}
// 根据Id删除用户
export function deletel(id) {
  return axios.delete('/users/delete' + id);
}
// 添加用户
export function addTo(addform) {
  return axios.post('/users/register', addform);
}
// 编辑用户
export function editList(email, phone, id) {
  return axios.patch('/users' + id, {
    email,
    phone,
  });
}
// 获取分类列表
export function DataList() {
  return axios.get('/catlist/lis');
}
// 添加一级分类
export function AddOneList(cat_name) {
  return axios.post('/catlist/addlist', {
    cat_name,
  });
}
// 添加二级分类
export function AddTwoList(cat_name, id) {
  return axios.post('/catlist/addlist' + id, {
    cat_name,
  });
}
// 根据ID查询一级分类
export function Inquire(id) {
  return axios.get('/catlist' + id);
}
// 修改一级分类
export function InquirePost(cat_name, id) {
  return axios.post('/catlist' + id, {
    cat_name,
  });
}
// 根据ID查询二级分类
export function InquireTwo(id) {
  return axios.get('/catlist/list' + id);
}
// 修改二级分类
export function InquireTwoPost(cat_name, id) {
  return axios.post('/catlist/lit' + id, {
    cat_name,
  });
}
// 删除一级分类
export function DeleteOne(id) {
  return axios.delete('/catlist/delete' + id);
}
// 删除二级分类
export function DeleteTwo(id) {
  return axios.delete('/catlist/deletes/' + id);
}
// 添加首页Banner图
export function uploadImage(file, progress, Addinput) {
  let formData = new FormData();
  formData.append('file', file);
  formData.append('title', Addinput);
  return service({
    url: 'http://192.168.31.14:9000/swipe',
    method: 'post',
    data: formData,
    onUploadProgress(event) {
      let v = Math.round((event.loaded / event.total) * 100);
      progress.value = v == 100 ? 80 : v;
    },
  });
}
// 获取Banner列表
export function BannerList() {
  return axios.get('/swipe');
}
// 根据Id删除Banner图
export function DeleteBanner(id) {
  return axios.delete('/swipe/' + id);
}
// 获取商品列表
export function CommodityList() {
  return axios.post('/goods/list');
}

export function AddUpload(ForData) {
  return axios.post('/goods/ce', ForData);
}
// 上架
export function YesJia(id) {
  return axios.post('/goods/' + id + '/on');
}
// 下架
export function NoJia(id) {
  return axios.post('/goods/' + id + '/off');
}
// 添加商品
export function AdDCommodity(FormData) {
  return axios.post('/goods/upload', FormData);
}
